from django.contrib.auth.mixins import LoginRequiredMixin, AccessMixin
from django.http import JsonResponse
from django.views import View
# class LoginRequiredJSONMixin(AccessMixin):
#     """验证用户是否登录的扩展类，未登录则返回JSON响应"""

#     def dispatch(self, request, *args, **kwargs):
#         if not request.user.is_authenticated:
#             return JsonResponse({'code': 400, 'errmsg': '请先登录'})
#         return super().dispatch(request, *args, **kwargs)

class LoginRequiredView(LoginRequiredMixin):
    # 重写handle_no_permission方法，返回JSON响应
    def handle_no_permission(self):
        return JsonResponse({'code': 400, 'errmsg': '请先登录'})